/****************************************************************************/
/*  File:       RepoResolver.java                                           */
/*  Author:     F. Georges - H2O Consulting                                 */
/*  Date:       2011-02-16                                                  */
/*  Tags:                                                                   */
/*      Copyright (c) 2011 Florent Georges (see end of file.)               */
/* ------------------------------------------------------------------------ */


package org.fgeorges.qizx.tests.resolver;

import com.qizx.api.ModuleResolver;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import javax.xml.transform.stream.StreamSource;
import org.expath.pkg.repo.PackageException;
import org.expath.pkg.repo.Repository;
import org.expath.pkg.repo.URISpace;

/**
 * Resolve a specific XQuery library module.
 *
 * @author Florent Georges
 * @date   2011-02-16
 */
public class RepoResolver
        implements ModuleResolver
{
    public RepoResolver(Repository repo)
    {
        myRepo = repo;
    }

    public URL[] resolve(String namespace, String[] locations)
            throws MalformedURLException
    {
        URI uri = null;
        try {
            StreamSource src = myRepo.resolve(namespace, URISpace.XQUERY);
            if ( src != null ) {
                uri = new URI(src.getSystemId());
                File f = new File(uri);
                return new URL[]{ f.toURL() };
            }
            return null;
        }
        catch ( PackageException ex ) {
            // TODO: Error management.
            String msg = "Error resolving the XQuery lib '" + namespace + "' in the repo";
            throw new RuntimeException(msg, ex);
        }
        catch ( URISyntaxException ex ) {
            String msg = "Repository returned an invalid URI: '" + uri + "'";
            // there is no way to set the exception cause
            throw new MalformedURLException(msg);
        }
    }

    private Repository myRepo;
}


/* ------------------------------------------------------------------------ */
/*  DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS COMMENT.               */
/*                                                                          */
/*  The contents of this file are subject to the Mozilla Public License     */
/*  Version 1.0 (the "License"); you may not use this file except in        */
/*  compliance with the License. You may obtain a copy of the License at    */
/*  http://www.mozilla.org/MPL/.                                            */
/*                                                                          */
/*  Software distributed under the License is distributed on an "AS IS"     */
/*  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See    */
/*  the License for the specific language governing rights and limitations  */
/*  under the License.                                                      */
/*                                                                          */
/*  The Original Code is: all this file.                                    */
/*                                                                          */
/*  The Initial Developer of the Original Code is Florent Georges.          */
/*                                                                          */
/*  Contributor(s): none.                                                   */
/* ------------------------------------------------------------------------ */
